//
//  CascadeData.m
//  etionUI
//
//  Created by wangjian on 11/21/13.
//  Copyright (c) 2013 GuangZhouXuanWu. All rights reserved.
//

#import "CascadeData.h"

@implementation CCascadeSelectorCellData

- (void)dealloc
{
    [_title release];
    [_szId release];
    [_parentId release];
    [_children release];
    [super dealloc];
}

- (BOOL)enumerateDataUsingBlock:(void (^)(CCascadeSelectorCellData *data, BOOL *stop))block
{
    __block BOOL stop = NO;
    
    block(self, &stop);
    if (stop == YES)
    {
        return stop;
    }
    for (NSUInteger i=0 ;i<self.children.count; i++)
    {
        CCascadeSelectorCellData *childData = [self.children objectAtIndex:i];
        stop = [childData enumerateDataUsingBlock:block];
        if (stop == YES)
        {
            break;
        }
    }
    
    return stop;
}

@end

@implementation CCascadeData

- (void)enumerateObjectsUsingBlock:(void (^)(CCascadeSelectorCellData *data, NSUInteger level, NSUInteger index, BOOL *stop))block
{
    [self enumerateData:self.rootData level:0 index:0 usingBlock:block];
}

- (BOOL)enumerateData:(CCascadeSelectorCellData *)data level:(NSUInteger)level index:(NSUInteger)index usingBlock:(void (^)(CCascadeSelectorCellData *data, NSUInteger level, NSUInteger index, BOOL *stop))block
{
    __block BOOL stop = NO;

    block(data, level, index, &stop);
    if (stop == YES)
    {
        return stop;
    }
    for (NSUInteger i=0 ;i<data.children.count; i++)
    {
        CCascadeSelectorCellData *childData = [data.children objectAtIndex:i];
        stop = [self enumerateData:childData level:level+1 index:i usingBlock:block];
        if (stop == YES)
        {
            break;
        }
    }
    
    return stop;
}

- (CCascadeSelectorCellData *)curSelectedCellData
{
    __block CCascadeSelectorCellData *selData = nil;
    [self enumerateObjectsUsingBlock:^(CCascadeSelectorCellData *data, NSUInteger level, NSUInteger index, BOOL *stop)
    {
        if (data.selected == YES)
        {
            selData = data;
            *stop = YES;
        }
    }];
    return selData;
}

@end
